package com.example.wenhua.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 日志工具类
 */
public class LogUtil {
    
    private static final Logger logger = LoggerFactory.getLogger(LogUtil.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    /**
     * 打印调试信息
     */
    public static void debug(String message, Object... args) {
        logger.debug(message, args);
    }
    
    /**
     * 打印信息
     */
    public static void info(String message, Object... args) {
        logger.info(message, args);
    }
    
    /**
     * 打印警告信息
     */
    public static void warn(String message, Object... args) {
        logger.warn(message, args);
    }
    
    /**
     * 打印错误信息
     */
    public static void error(String message, Object... args) {
        logger.error(message, args);
    }
    
    /**
     * 打印对象的JSON格式
     */
    public static void printJson(String prefix, Object obj) {
        try {
            String json = objectMapper.writeValueAsString(obj);
            logger.info("{}: {}", prefix, json);
        } catch (Exception e) {
            logger.error("Failed to convert object to JSON: {}", e.getMessage());
        }
    }
    
    /**
     * 打印方法执行时间
     */
    public static void logExecutionTime(String methodName, long startTime) {
        long endTime = System.currentTimeMillis();
        logger.info("Method [{}] executed in {} ms", methodName, (endTime - startTime));
    }
}